package cn.jishiyu11.xeqb.activity.personalCenterActivitys

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.base.BaseActivity
import cn.jishiyu11.xeqb.http.http.i.httpbase.VIP_PAY_ACT
import cn.jishiyu11.xeqb.http.http.i.httpbase.VIP_PAY_HISTORY
import cn.jishiyu11.xeqb.http.http.i.httpbase.ZF_ODERINFO
import cn.jishiyu11.xeqb.utils.USER_UserNo
import cn.jishiyu11.xeqb.webview.X5WebViewActivity
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.act_webview.*
import kotlinx.android.synthetic.main.tab_activity_fra.*

/**
 * Created by jsy_zj on 2018-03-05.
 * VIP 购买界面
 */
class VIPMember : BaseActivity() {

    private var url = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_webview)
        url = intent.getStringExtra("url")
        findViewById()
        initView()
    }

    override fun findViewById() {
        title_image.visibility = View.VISIBLE
        title_image.setOnClickListener { finish() }
        title_complete.setOnClickListener {
            var intent = Intent(this,X5WebViewActivity::class.java)
            intent.putExtra("url",VIP_PAY_HISTORY)
            startActivity(intent)
        }

//        title_view.text = getString(R.string.personalcenter_vip)
        title_complete.visibility = View.VISIBLE
        title_complete.text = getString(R.string.personalcenter_vip_history)
        banner_linear.visibility = View.VISIBLE
        banner_webview!!.isHorizontalScrollBarEnabled = false
        banner_webview!!.isVerticalScrollBarEnabled = false
        //下面方法去掉
        val ix5 = banner_webview!!.x5WebViewExtension
        ix5?.setScrollBarFadingEnabled(false)
        if (url.contains("?")){
            url = "$url&userID=$USER_UserNo&appID=110"
        }else{
            url = "$url?userID=$USER_UserNo&appID=110"
        }
    }

    private val webViewClient = object : WebViewClient() {

//        override fun shouldOverrideUrlLoading(view: WebView?, pl: String?): Boolean {
//            if (url!!.startsWith("http") || url!!.startsWith("https")) { //http和https协议开头的执行正常的流程
//                return false
//            } else {
//                //其他的URL则会开启一个Acitity然后去调用原生APP
//                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
//                if (intent.resolveActivity(packageManager) == null) {
//                    //说明系统中不存在这个activity
//                } else {
//                    //如果想要加载成功跳转可以 这样
//                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
//                    startActivity(intent)
//                }
//                return true
//            }
//        }

        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            val title = view!!.title
            if (TextUtils.isEmpty(title)
                    || title.contains("404")
                    || title.contains("找不到")
                    || title.contains("无法打开")) {
                title_view!!.text = title
                banner_linear!!.visibility = View.VISIBLE
            } else {
                banner_linear!!.visibility = View.VISIBLE
                title_view!!.text = title
            }
        }

        override fun onReceivedError(view: WebView?, errorCode: Int, description: String?, failingUrl: String?) {
            super.onReceivedError(view, errorCode, description, failingUrl)
        }
    }

    private val webChromeClient = object : WebChromeClient() {

        override fun onReceivedTitle(view: WebView?, title: String?) {
            super.onReceivedTitle(view, title)
            if (TextUtils.isEmpty(title)
                    || title!!.contains("404")
                    || title.contains("找不到")
                    || title.contains("无法打开")) {
                    title_view!!.text = title
                banner_linear!!.visibility = View.GONE
            } else {
                banner_linear!!.visibility = View.VISIBLE
                    title_view!!.text = view!!.title
            }
        }


        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            banner_progressBar!!.progress = newProgress
            if (newProgress == 100) {
                banner_progressBar!!.visibility = View.GONE
            } else {
                banner_progressBar!!.visibility = View.VISIBLE
                view!!.visibility = View.VISIBLE
            }
        }
    }

    override fun initView() {
        banner_webview.webChromeClient = webChromeClient
        banner_webview.webViewClient = webViewClient
        if (!TextUtils.isEmpty(url) && "null" != url) {
            banner_linear!!.visibility = View.VISIBLE
            banner_webview!!.loadUrl(url)
        } else {
            title_view!!.text = ""
            banner_linear!!.visibility = View.GONE
        }

    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

}